<?php
include_once('./config/config.php');
// 接收参数id
$id = $_GET['id'];
// echo $id;
// 通过id获取信息
$sql = "select * from department where id = '$id'";
$admin = findOne($sql);
// var_dump($admin);

if ($_POST) {
  
  if ($_POST['action'] = 'depname') {
    $name = $_POST['name'];
    $data = findOne("select * from department where name = '$name'");
    if ($data) {
      error("该部门已存在");
    } else {
      success("该部门名称可以使用", $data);
    }
  }
  
  $name = $_POST['depname'];
  $sql = "select * from department where name = '$name'";
  $res = findOne($sql);
  if (!$res) {
    $data = [
      'name' => $name
    ];
    $i = update('department', $data, "id = '$id'");
    if ($i) {
      ShowMsg("修改部门成功", "departmentList.php");
      exit;
    } else {
      ShowMsg("修改部门失败");
      exit;
    }
  } else {
    ShowMsg("该部门已存在");
    exit;
  }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <!-- 引入 -->
  <?php include_once('./common/meta.php') ?>
</head>

<style>
  .avatar_lab {
    width: 200px;
  }

  .avatar_img {
    width: 200px;
  }
</style>

<body>
  <!-- 引入头部导航 -->
  <?php include_once('./common/navbar.php') ?>
  <!-- 引入侧边菜单栏 -->
  <?php include_once('./common/sidebar.php') ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">编辑部门</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li class="active">editDepartment</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">
        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='departmentList.php'"><i class="icon-list"></i> 返回列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="POST" enctype="multipart/form-data">
                <!-- 部门名称 -->
                <label>部门名称</label>
                <input type="text" class="input-xxlarge" name="depname" id="depname" value="<?php echo $admin['name'] ?>" required>
                <span id="msg"></span>
                <br>
                <button class="btn btn-primary" id="subBtn" value="提交">提交</button>
              </form>
            </div>
          </div>
        </div>

        <!-- 引入底部 -->
        <?php include_once('./common/footer.php') ?>

      </div>
    </div>
  </div>

</body>

</html>
<!-- 引用公共js文件 -->
<?php include_once('./common/script.php') ?>

<script>
  $(document).ready(function() {
    $('#depname').change(function()
    {
      let name = $(this).val()
      $.ajax({
        type: "POST",
        url: 'editDepartment.php',
        data: {
          name,
          action: 'depname'
        },
        dataType: 'json',
        success: function(res) {
          if (res.code == 1) {
            $("#msg").text('').css({"color": "blue"})
          } else {
            $("#msg").text(res.msg).css({"color": "red"})
          }
        },
        error: function (err) { 
          console.log(err)
        }
      })
    }) 
  })
</script>
